<?php
if(!defined('INSIDE')) {
	exit('Access Denied');
}

class controller {
	public static function execute() {
		controller::_do_init();
		controller::_do_action();
		controller::_do_result();
	}
	
	private static function _do_init() {
		global $_GVAR;
		
		$_GVAR['cur_ctrl'] = CUR_CTRL;
		$cur_act = util::getgpc('act');
		if(empty($cur_act)){
			$_GVAR['cur_act'] = 'index';
		}
		else {
			$_GVAR['cur_act'] = $cur_act;
		}
	}
	
	private static function _do_action() {
		global $_GVAR;
		//var_dump($_GVAR);
		
		$controller = null;
		$action = null;
		if(isset($_GVAR['cur_ctrl'])) {
			$controller = $_GVAR['cur_ctrl'];
		}
		else {
			exit('Missing controller');
		}
		if(isset($_GVAR['cur_act'])) {
			$action = isset($_GVAR['cur_act']);
		}
		else {
			exit('Missing action');
		}
		
		$modfile = util::file_abs('ctrl', $controller);
		if(!empty($modfile)){
			include $modfile;
		}
		else{
			exit('Missing controller: '.$controller);
		}
	}
	
	private static function _do_result() {
		global $_GVAR;
		
		$tplfile = null;
		if(isset($_GVAR['action_result'])) {
			$tplfile = $_GVAR['action_result'];
		}
		else {
			exit('Missing action result');
		}
		$tpl_hdl = new tpl_loader();
		include $tpl_hdl->load_tpl($tplfile);
	}
}